#include <bits/stdc++.h>
using namespace std;


class Solution {
typedef long long LL;
public:
	static const int N = 20;

	int trans(LL x, int res[])
	{
		int n = 0;
		while (x)
		{
			res[n++] = x % 10;
			x /= 10;
		}
		reverse(res, res+n);
		return n;
	}

	int extend(int a[], int n, bool odd)
	{
		int st = n-1;
		if (odd) --st;
		for (int i=st; i>=0; --i)
			a[n++] = a[i];
		return n;
	}

	LL trans(int a[], int n)
	{
		LL res = 0;
		for (int i=0; i<n; ++i)
			res = res * 10 + a[i];
		return res;
	}

	void update(int a[], int n, LL &ans, LL v)
	{
		if (n > 18) return;
		if (n < 0) return;
		LL resv = trans(a, n);
		// dbg(resv);
		if (resv == v) return;
		// fprintf(stderr, "resv = %lld, ans = %lld\n", resv, ans);
		if (llabs(resv-v) < llabs(ans-v) || (llabs(resv-v) == llabs(ans-v) && resv < ans))
			ans = resv;
	}

	string go(LL x)
	{
		char a[N];
		int n = 0;
		while (x)
		{
			a[n++] = x % 10 + '0';
			x /= 10;
		}
		a[n] = 0;
		return string(a);
	}

    string nearestPalindromic(string n) {
    	int m = n.length();
    	string ans = n;
    	for (int i=m-1; i>=0; --i)
    		ans[i] = ans[m-i-1];
    	
    	int a[N]; memset(a, 0, sizeof a);
    	for (int i=0; i<m; ++i)
    		a[i] = n[i] - '0';
    	LL nv = trans(a, m), ansv = -1e18;

    	LL x = trans(a, (m + 1) / 2);
    	for (int i=0; i<=100; ++i)
    	{
    		LL y = x + i;
    		 int a[N]; memset(a, 0, sizeof a);
    		int n = trans(y, a);
    		extend(a, n, true);
    		update(a, n * 2 - 1, ansv, nv);

    		extend(a, n, false);
    		update(a, n * 2, ansv, nv);

    		extend(a, n-1, true);
    		update(a, n * 2 - 3, ansv, nv);
    		extend(a, n-1, false);
    		update(a, n * 2 - 2, ansv, nv);

    		y = x - i;
    		n = trans(y, a);
    		extend(a, n, true);
    		update(a, n * 2 - 1, ansv, nv);

    		extend(a, n, false);
    		update(a, n * 2, ansv, nv);

    		
    		extend(a, n-1, true);
    		update(a, n * 2 - 3, ansv, nv);
    		extend(a, n-1, false);
    		update(a, n * 2 - 2, ansv, nv);
    	}
    	string res = go(ansv);
    	if (ansv == 0) res = "0";
    	return res;
    }
};

int main(int argc, char const *argv[])
{
	Solution s;
	cout << s.nearestPalindromic("99999") << endl;
	return 0;
}